//###############
// FILE : GUIInitializer.java
// WRITER : Lior Zimmerman liorz06 066462300
// WRITER : Ohad Dan ohad_dan 036767960
// EXERCISE : oop ex3 2008
// DESCRIPTION:
// Responsible of initializing the gui.
//###############
package oop.ex3.fileSearchView;
import java.awt.*;
import java.io.IOException;

import javax.swing.*;
/**
 * Responsible of initializing the gui.
 */
public class GUIInitializer extends JFrame implements GUIView {
	private static final long serialVersionUID = 1L;
	//********************************************************************
	//  Data members :                                                   *
	//********************************************************************
	private JTabbedPane _tabbedPane;
    private SearchTab _searchTab;
    private SetupTab _setupTab;
    private LogTab _logTab;
    private JLabel _lblStatus;
    //****************** *************************************************
	//  Finals :                                                         *
	//********************************************************************
    public static final String STATUS_READY = "Status: Ready";
    private final String GUI_MAIN_LBL = "Advanced file search";
    private final String SRCH_TAB_LBL = "Search";
    private final String STUP_TAB_LBL = "Setup";
    private final String LOG_TAB_LBL = "Log";
    private final int MAIN_FRAME_WIDTH = 500;
    private final int MAIN_FRAME_HEIGHT = 400;
    
    //********************************************************************
	//  Methods :                                                        *
	//********************************************************************	
    /**
     * Constructs a new gui initializer.
     */
    public GUIInitializer(){
    	// Create new frame.
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle( GUI_MAIN_LBL );
        setSize(MAIN_FRAME_WIDTH,MAIN_FRAME_HEIGHT);
        JPanel topPanel = new JPanel();
        topPanel.setLayout( new BorderLayout() );
        getContentPane().add( topPanel );        
        // Create all tabs.
        _tabbedPane = new JTabbedPane();
        _searchTab = new SearchTab(this);
        _setupTab = new SetupTab(this);
        _logTab = new LogTab(this);
        // Add tabs.
        _tabbedPane.addTab(SRCH_TAB_LBL, _searchTab );
        _tabbedPane.addTab(STUP_TAB_LBL, _setupTab );
        _tabbedPane.addTab( LOG_TAB_LBL, _logTab );
        // Add other components.
        topPanel.add( _tabbedPane, BorderLayout.CENTER );
        _lblStatus = new JLabel(STATUS_READY,JLabel.CENTER);
        topPanel.add(_lblStatus,BorderLayout.PAGE_END);
    }
    /**
     * Returns search tab.
     * @return Search tab.
     */
    public SearchTabInterface getSearchView() {
        return _searchTab;
    }
    /**
     * Returns log tab.
     * @return Log tab.
     */
    public LogTabInterface getLogView() {
        return _logTab;
    }
    /**
     * Returns Setup tab.
     * @return Setup tab.
     */
    public SetupTabInterface getSetupView() {
        return _setupTab;
    }
    /**
     * Sets listener factory for log tabs.
     */
    public void setListeners() {
    	_logTab.setActionListeners();
    	_setupTab.setActionListeners();
    	_searchTab.setActionListeners();
    }
    /**
     * Returns current program status.
     * @return Current program status.
     */
    public String getStatus() {
        return _lblStatus.getText();
    }
    /**
     * Sets program status.
     * @param str Text to be set at the status label.
     */
    public void setStatus(String str){
    	_lblStatus.setText(str);
    }
    /**
     * Opens save dialog and returns the selected file\directory path.
     * @return Path of the selected file\directory.
     * @throws IOException In case of read error. 
     */
    public String showSaveDialog() throws IOException{
        JFileChooser chooser = new JFileChooser();
        int val = chooser.showSaveDialog(this);
        if ( val == JFileChooser.APPROVE_OPTION ) {
            return chooser.getSelectedFile().getCanonicalPath();
        }
        return "";
    }
    /**
     * Opens save dialog and returns the selected file\directory path.
     * @param constraints directory\file only.
     * @return Path of the selected file\directory.
     * @throws IOException In case of read error. 
     */
    public String showOpenDialog( int constraints ) throws IOException {
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(constraints);
        int val = chooser.showOpenDialog(this);
        if ( val == JFileChooser.APPROVE_OPTION ) {
            return chooser.getSelectedFile().getCanonicalPath();
        }
        return null;
    }  
}